package com.gxa.finance.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gxa.common.commons.R;
import com.gxa.finance.pojo.CollectionAccount;
import com.gxa.finance.service.CollectionAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@Component
@RestController
@Slf4j
@RequestMapping("/finance/collectionAccount")
public class CollectionAccountController {

    @Autowired
    CollectionAccountService collectionAccountService;


    //添加收款账户
    @PostMapping(value = "/addCollectionAccount")
    public R addCollectionAccount(@RequestBody CollectionAccount collectionAccount){

        R r = new R();
        try {
            collectionAccountService.save(collectionAccount);
            r.setCode(200);
            r.setMsg("success");
            log.info("添加收款账户成功");
        }
        catch (Exception e){
            e.printStackTrace();
            r.setCode(401);
            r.setMsg("系统开小差，请联系管理员");
            log.info("添加收款账户失败");
        }
        return r;
    }


    //删除收款账户
    @PostMapping(value = "/delCollectionAccount")
    public R delCollectionAccount(String account){

        R r = new R();
        try {
            QueryWrapper<CollectionAccount> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("account",account);
            collectionAccountService.remove(queryWrapper);
            r.setCode(200);
            r.setMsg("success");
            log.info("删除收款账户成功");
        }
        catch (Exception e){
            e.printStackTrace();
            r.setCode(401);
            r.setMsg("系统开小差，请联系管理员");
            log.info("删除收款账户失败");
        }
        return r;
    }


    //修改账户余额
    @PostMapping(value = "/updateAccountMoney")
    public R updateAccountMoney(BigDecimal accountMoney){

        R r = new R();
        try {
            UpdateWrapper<CollectionAccount> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("account","66668888").setSql("account_money = account_money+"+accountMoney);
            collectionAccountService.update(updateWrapper);
            r.setCode(200);
            r.setMsg("success");
            log.info("修改账户余额成功");
        }
        catch (Exception e){
            e.printStackTrace();
            r.setCode(401);
            r.setMsg("系统开小差，请联系管理员");
            log.info("修改账户余额失败");
        }
        return r;
    }

}
